package com.xls.box2023.utils;

import android.app.Activity;
import android.text.TextUtils;

import com.lzz.bottomencapsulation.entity.VersionEntity;
import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.lzz.bottomencapsulation.utils.AppUtils;
import com.lzz.bottomencapsulation.utils.LUtils;
import com.lzz.bottomencapsulation.utils.TUtils;
import com.xls.box2023.BoxAppLication;
import com.xls.box2023.entity.UserEntity;
import com.xls.box2023.home.HomeNetWorkUtil;
import com.xls.resource.dialog.APKInsDialog;
import com.xls.resource.dialog.VersionDialog;
import com.xls.resource.dialog.utils.download.PackageUtil;

import java.io.File;

import static android.os.Environment.DIRECTORY_DOWNLOADS;

public class VersionUtil {
    /**
    * 单例模式
    **/
    private static VersionUtil mVersionUtil = null;

    private String FilePath=BoxAppLication.getBaseApplication().getAppManager().currentActivity().getExternalFilesDir(DIRECTORY_DOWNLOADS)+ File.separator;
        /**
         * 构造函数私有化
         **/
        private VersionUtil() {
        }
        /**
         * 公有的静态函数，对外暴露获取单例对象的接口
         **/
        public static VersionUtil getInstance() {
            if (mVersionUtil == null) {
                synchronized (VersionUtil.class) {
                    if (mVersionUtil == null) {
                        mVersionUtil = new VersionUtil();
                    }
                }
            }
            return mVersionUtil;
        }


    /**
     * 检测版本更新
     */
    String channel="";
    public void checkUpdate(Activity activity) {
        HomeNetWorkUtil.getInstance().getUserAgentInfo(new RequestCallBack<UserEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onRequestSuccess(UserEntity requestData) {
                channel=requestData.getUserId();
                if (TextUtils.isEmpty(channel)){
                    TUtils.showShort(activity,"没有上级联系人，无法下载游戏，请联系游戏代理");
                    return;
                }
                HomeNetWorkUtil.getInstance().checkUpdate(new RequestCallBack<VersionEntity>() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onStop() {

                    }

                    @Override
                    public void onRequestSuccess(VersionEntity requestData) {
                        if (requestData==null)return;
                        if (isUpdataVersion(requestData.getVersionCode())){
                            String filepath=FilePath+requestData.getFileName();
                            requestData.setFilePath(filepath);
                            File file= new File(filepath);
                            LUtils.e(file.length());
//                    判断本地是否有安装包,和线上安装包是否一致
                            if (file.exists()&&file.length()==requestData.getFileSize()&& PackageUtil.computeMD5(new File(filepath)).toUpperCase().equals(requestData.getMd5().toUpperCase())){
                                showAPKInsDialog(requestData);
                            }else {
                                if (file.exists())file.delete();
                                showVersionDialog(requestData,channel); //弹窗  下载更新
                            }
                        }
                    }

                    @Override
                    public void onRequestfail(String errno, String errmsg) {

                    }
                });
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {
                TUtils.showShort(activity,errmsg);
            }
        });

    }
    APKInsDialog apkInsDialog;
    private void showAPKInsDialog(VersionEntity requestData) {
        if (versionDialog!=null)versionDialog.dismiss();
        apkInsDialog=new APKInsDialog(BoxAppLication.getBaseApplication().getAppManager().currentActivity());
        apkInsDialog.setVersionEntity(requestData);
        apkInsDialog.show();
    }
    VersionDialog versionDialog;
    private void showVersionDialog(VersionEntity requestData,String channel) {
        if (apkInsDialog!=null)apkInsDialog.dismiss();
        versionDialog=new VersionDialog(BoxAppLication.getBaseApplication().getAppManager().currentActivity(), requestData,channel);
        versionDialog.show();
    }

    /**
     * 网络版本与当前版本是否一致  一致不需要更新
     * @param versioncode
     * @return
     */
    private boolean isUpdataVersion(long versioncode) {
        LUtils.e( AppUtils.getVersionCode(BoxAppLication.getBaseApplication()));
        LUtils.e(versioncode);
        return versioncode!= AppUtils.getVersionCode(BoxAppLication.getBaseApplication());
    }

    public void onComplete(){
        if (versionDialog!=null&&versionDialog.getDownloadUtils()!=null&&versionDialog.getDownloadUtils().getObservable()!=null){
            versionDialog.getDownloadUtils().getObservable().dispose();
        }
    }
}
